package com.example.smartcity_poverty

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Application
import android.content.Context
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
import java.util.regex.Pattern

/**
 *  created by ikun
 *  ANDROID STUDIO - version 4.0
 *  email: jinkunwei41@gmail.com
 * --------------------------------
 *  全局获取Context
 **/

var toShowAlertDialog = """
    bug1: 手机上显示新闻评论失败! 模拟器正常显示...
    bug2: 无法发表评论(后续解决)
""".trimIndent()

class App : Application() {

    companion object {
        const val url = "http://124.93.196.45:10001/"

        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        // 获取Token
        val getTokenStr: String
            get() = context.getSharedPreferences("token", Context.MODE_PRIVATE)
                .getString("token", "").toString()

        // 获取Token
        fun getToken(): String = context.getSharedPreferences("token", Context.MODE_PRIVATE)
            .getString("token", "").toString()


        // 判断是否第一次
        fun isStart(): Boolean {
            return context.getSharedPreferences("token", Context.MODE_PRIVATE)
                .getBoolean("true", false)
        }

        // 弹窗提示
        fun startAppShow(context: Context): AlertDialog =
            AlertDialog.Builder(context)
                .setMessage(toShowAlertDialog)
                .setNegativeButton("ok") {_, _ -> {}}
                .show()

        /**
         * 动画，
         * 从左到右的动画
         */
        fun getAnimationEndAndStart(): Animation = animationUtils(150f, 0f)

        /**
         * 动画
         * 从下往上
         */
        fun getAnimationBottomAndTop(): Animation = animationUtils(0f, 200f)

        /**
         * 动画
         * 从上往下
         */
        fun getAnimationTopAndBottom(): Animation = animationUtils(0f, -150f)

        /**
         * 从左往右
         */
        fun getAnimationLeftAndEnd(): Animation = animationUtils(-150f, 0f)

        /**
         * 从下往上
         */
        fun getAnimationLogin() = AnimationSet(true).apply {
            this.addAnimation(TranslateAnimation(0f, 0f, 600f, 0f).apply {
                this.duration = 600
                this.repeatCount = 0
                this.repeatMode = Animation.RESTART
            })
        }


        /**
         * 封装动画
         */
        private fun animationUtils(left: Float, end: Float): Animation = AnimationSet(true).apply {
            // 从左到右的动画
            this.addAnimation(
                TranslateAnimation(left, 0f, end, 0f).apply {
                    this.duration = 700
                    this.repeatCount = 0
                    this.repeatMode = Animation.RESTART
                }
            )
            // 渐变
            this.addAnimation(
                AlphaAnimation(0f, 10f).apply {
                    this.duration = 700
                    this.repeatCount = 0
                    this.repeatMode = Animation.RESTART
                }
            )
        }


        // 新闻详情内容显示照片问题
        fun addContentImgURL(htmlContent: String, serverAddress: String): String {
            val imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"
            val pattern = Pattern.compile(imgRegex)
            val matcher = pattern.matcher(htmlContent)

            val updatedHtmlContent = StringBuffer()

            while (matcher.find()) {
                val src = matcher.group(1)
                val updatedSrc = if (!src.startsWith("http")) {
                    "$serverAddress$src"
                } else {
                    src
                }
                matcher.appendReplacement(updatedHtmlContent, "<img src=\"$updatedSrc\">")
            }

            matcher.appendTail(updatedHtmlContent)

            return updatedHtmlContent.toString()
        }

    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
    }
}